﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Entities;
using DiemChapNhanThe.Data;
using System.Text;

public partial class Lien_He : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ltrTieude.Text = "Liên Hệ";
        }
    }
    protected void ImageBtnRefresh_Click(object sender, ImageClickEventArgs e)
    {
    }
    //Su kien khi nhan nut goi
    protected void BtnGui_Click(object sender, EventArgs e)
    {
        this.CaptchaControl.ValidateCaptcha(this.txtCapcha.Text);
        //Capcha dung
        if (this.CaptchaControl.UserValidated)
        {
            bool updateStatus = false;
            LienHe ent_LienHe = new LienHe();
            ent_LienHe.NguoiGui = txtNguoiGoi.Text;
            ent_LienHe.DiaChi = txtDiaChi.Text;
            ent_LienHe.ThuDienThu = txtEmail.Text;
            ent_LienHe.DienThoai = txtDienThoai.Text;
            ent_LienHe.NoiDung = txtNoiDung.Text;
            ent_LienHe.TrangThai = false;
            ent_LienHe.NgayCapNhat = Common.GetServerDateTime();
            //Chen lien he vao csdl
            updateStatus = DataRepository.LienHeProvider.Insert(ent_LienHe);
            //chen thanh cong
            if (updateStatus)
            {
                ShowPopUpMsg("Gởi liên hệ thành công!");
                // Ve trang chu
                RedirectTo("~/");

            }
            //chen khong thanh cong
            else
            {
                ShowPopUpMsg("Gửi Liên hệ không thành công!");
            }
        }
        //capcha sai
        else
        {
            ShowPopUpMsg("Mã xác nhận không chính xác!");
        }
    }
    //chuyen trang
    private void RedirectTo(string url)
    {
        string redirectURL = Page.ResolveClientUrl(url);
        string script = String.Format("window.location = '{0}';", redirectURL);
        ScriptManager.RegisterStartupScript(this, typeof(Page), "RedirectTo", script, true);
    }
    //nhan nut huy bo
    protected void BtnHuy_Click(object sender, EventArgs e)
    {
        txtNguoiGoi.Text ="";
        txtDiaChi.Text ="";
        txtEmail.Text ="";
        txtDienThoai.Text ="";
        txtNoiDung.Text ="";
        txtCapcha.Text = "";
    }
    //Hien thi popup
    private void ShowPopUpMsg(string msg)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("alert('");
        sb.Append(msg.Replace("\n", "\\n").Replace("\r", "").Replace("'", "\\'"));
        sb.Append("');");
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
    }
}
